﻿using AutoMapper;
using FarmSmartSleep.Common.Domain.RBACFile;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using R.FarmSmartSleep.RBAC.API.Common.R.Dtos;
using System.Security;

namespace R.FarmSmartSleep.RBAC.API.Apliction.Command
{
    public class RouterIndexHandler : IRequestHandler<RouterIndexCommon, ResultT<List<RolterIndexDto>>>
    {
        private readonly IBaseRepository<Employee> emploveeRep;
        private readonly IBaseRepository<Roles> rolesRep;
        private readonly IBaseRepository<EmployeeRole> employeeRolesRep; 
        private readonly IMapper mapper;
        private readonly IBaseRepository<Permission> permissionRep; 
        private readonly IBaseRepository<RolesPermissions> rolesPermissionRep;

        public RouterIndexHandler(IBaseRepository<Employee> emploveeRep, IBaseRepository<Roles> rolesRep, IBaseRepository<EmployeeRole> employeeRolesRep, IMapper mapper, IBaseRepository<Permission> permissionRep, IBaseRepository<RolesPermissions> rolesPermissionRep)
        {
            this.emploveeRep = emploveeRep;
            this.rolesRep = rolesRep;
            this.employeeRolesRep = employeeRolesRep;
            this.mapper = mapper;
            this.permissionRep = permissionRep;
            this.rolesPermissionRep = rolesPermissionRep;
        }

        public async Task<ResultT<List<RolterIndexDto>>> Handle(RouterIndexCommon request, CancellationToken cancellationToken)
        {
            var result = new ResultT<List<RolterIndexDto>>() { Code = (int)CodeEnum.search };
            try
            {
                var emplovee = emploveeRep.GetAll().ToList();
                var employeeRoles = employeeRolesRep.GetAll().ToList();
                var RolesPermission = rolesPermissionRep.GetAll().ToList();
                var Permission = permissionRep.GetAll().ToList();
                var Roles = rolesRep.GetAll().ToList();
                var list = (from e in emplovee
                            join er in employeeRoles
                            on e.EmployeeId equals er.Id
                            join r in Roles
                           on er.RolesId equals r.RolesId
                            where e.EmployeeId == request.EmployeeId
                            join rp in RolesPermission
                           on r.RolesId equals rp.RolesId
                            join p in Permission
                            on rp.PermissionsId equals p.PermissionsId
                            where p.Deletes == false
                            select new RolterIndexDto
                            {
                                PermissionsId = p.PermissionsId,
                                PermissionsName = p.PermissionsName,
                                URL = p.URL,
                                PerantId = p.PerantId,
                            }).ToList();
              if(list.Count()>0)
                { 
                    result.Item = list;
                    result.Code = (int)CodeEnum.success;
                    result.Messege = "查询成功";
                }
              else
                {
                    result.Messege = "查询失败";
                }
            }
            catch (Exception)
            {
                result.Messege = "查询异常";
                throw;
            }
            return result;
        }
    }
}
